#include "movementdetection.h"

//bool movementDetection(unsigned char* pImgPast, unsigned char* pImgNow, int size)
//{
//    int i = 0;
//    int sum = 0;
//    for(i; i<size; i++)
//    {
//        sum += pImgNow[i]-pImgPast[i];
//    }
//    if(sum >= THRESHOLD)
//    {
//        return true;
//    }
//    return false;
//}


int movementDetection(const QImage &past, const QImage &now)
{
    int x,y;
    int sum = 0;
    QRgb* pline;
    QRgb* nline;
    for(y = 0; y<past.height();y++)
    {
        pline = (QRgb* )past.scanLine(y);
        nline = (QRgb* )now.scanLine(y);
        for(x = 0; x<past.width(); x++)
        {
            if(((abs(qRed(pline[x]) - qRed(nline[x])) + abs(qBlue(pline[x]) - qBlue(nline[x])) +abs(qGreen(pline[x]) - qGreen(nline[x])))>30))
                sum++;
            //pr = qRed(pline[x]);
            //nr = qRed(nline[y]);
//            cpast = QColor(past.pixel(x,y));
//            cnow = QColor(now.pixel(x,y));
//            pr = cpast.red();
//            nr =cnow.red();
//            pg = cpast.green();
//            ng = cnow.green();
//            nb = abs(cpast.red() - cnow.red()) + abs(cpast.blue() - cnow.blue()) +abs(cpast.green() - cnow.green());
//            if(abs(cpast.red() - cnow.red()) + abs(cpast.blue() - cnow.blue()) +abs(cpast.green() - cnow.green()))
//                sum++;
        }

    }
    return sum;
}
